package com.ophiux.micro.core.application;

import com.ophiux.micro.core.domain.DomainResult;

public class Result<T> {
	/***静态集合***/
	public static String RESULT_SUCCESS = "success";
	public static String RESULT_FAIL = "fail";
	public static String RESULT_EXCEPTION = "exception";
	
	private String RESULT;	//结果
	
	private String TIMESTAMP;	//时间戳
	
	private String ERROR_CODE;	//错误码
	
	private String ERROR_MSG;	//错误消息描述
	
	private String SIGN;	//签名
	
	private T RESPONSE;
	
	
	
	
	public Result(String rESULT, String tIMESTAMP,String sIGN, String eRROR_CODE, String eRROR_MSG, T rESPONSE) {
		super();
		RESULT = rESULT;
		TIMESTAMP = tIMESTAMP;
		ERROR_CODE = eRROR_CODE;
		ERROR_MSG = eRROR_MSG;
		RESPONSE = rESPONSE;
		SIGN = sIGN;
	}
	
	public Result(String rESULT, String eRROR_CODE, String eRROR_MSG, T rESPONSE) {
		this( rESULT,  "", "",  eRROR_CODE,  eRROR_MSG,  rESPONSE);
	}

	public Result() {
		super();
	}

	public String getRESULT() {
		return RESULT;
	}

	public String getSIGN() {
		return SIGN;
	}

	public void setSIGN(String sIGN) {
		SIGN = sIGN;
	}

	public void setRESULT(String rESULT) {
		RESULT = rESULT;
	}


	public String getTIMESTAMP() {
		return TIMESTAMP;
	}

	private void setTIMESTAMP(String tIMESTAMP) {
		TIMESTAMP = tIMESTAMP;
	}


	public String getERROR_CODE() {
		return ERROR_CODE;
	}


	public void setERROR_CODE(String eRROR_CODE) {
		ERROR_CODE = eRROR_CODE;
	}





	public String getERROR_MSG() {
		return ERROR_MSG;
	}

	public void setERROR_MSG(String eRROR_MSG) {
		ERROR_MSG = eRROR_MSG;
	}

	public T getRESPONSE() {
		return RESPONSE;
	}




	public void setRESPONSE(T rESPONSE) {
		RESPONSE = rESPONSE;
	}
	
	public static <T> Result<T> success(){
		Result<T> result = new Result<T>(RESULT_SUCCESS, "100", "业务成功", null);
		return result;
	}
	
	public static <T> Result<T> success(T _response){
		Result<T> result = new Result<T>(RESULT_SUCCESS, "100", "业务成功", _response);
		return result;
	}
	
	public static <T> Result<T> success(T _response,String _error_message){
		Result<T> result = new Result<T>(RESULT_SUCCESS, "100", _error_message, _response);
		return result;
	}
	
	
	
	/**
	 * Application层，返回错误信息
	 * @param resultEnum
	 * @return
	 */
	public static <T> Result<T> fail(ResultEnum resultEnum){
		Result<T> result = new Result<T>(RESULT_FAIL, resultEnum.getCode() , resultEnum.getMsg() , null);
		return result;
	}
	public static <T> Result<T> fail(String _error_code , String _error_message){
		Result<T> result = new Result<T>(RESULT_FAIL, _error_code , _error_message , null);
		return result;
	}
	
	
	/**
	 * 说明：Application层，返回异常信息<br>
	 * 创 建 人: <b>hehailong</b>
	 * @param resultEnum
	 * @return
	 */
	public static <T> Result<T> exception(ResultEnum resultEnum){
		Result<T> result = new Result<T>(RESULT_EXCEPTION, resultEnum.getCode() , resultEnum.getMsg() , null);
		return result;
	}
	
	public static <T> Result<T> exception(String _error_code , String _error_message){
		Result<T> result = new Result<T>(RESULT_EXCEPTION, _error_code , _error_message , null);
		return result;
	}
	
	//是否成功
	public boolean isSuccess() {
		return this.getRESULT().equals(RESULT_SUCCESS) ? true : false;
	}
	
	
	/**
	 * 说明：Application层调application层，判断是失败还是异常<br>
	 * 创 建 人: <b>hehailong</b>
	 * @param result
	 * @return
	 */
	public static Result goBack(Result result) {
		if(RESULT_FAIL.equals(result.getRESULT())) {
			return Result.fail(result.getERROR_CODE(), result.getERROR_MSG());
		}else if(RESULT_EXCEPTION.equals(result.getRESULT())){
			return Result.exception(result.getERROR_CODE(), result.getERROR_MSG());
		}
		return result;
	}
	
	/**
	 * 说明：Application层调Domain层，判断是失败还是异常<br>
	 * 创 建 人: <b>hehailong</b>
	 * @param result
	 * @return
	 */
	public static Result goBack(DomainResult result) {
		if("102".equals(result.getErrorCode())) {
			return Result.exception(result.getErrorCode(), result.getMsg());
		}else{
			return Result.fail(result.getErrorCode(), result.getMsg());
		}
	}
	
}
